package it.unipi.di.rasip.gui.chat;

import java.util.Vector;

import it.unipi.di.rasip.Chat;
import it.unipi.di.rasip.Rasip;
import it.unipi.di.rasip.RasipUser;
import it.unipi.di.rasip.gui.RASDisplayable;
import it.unipi.di.rasip.gui.tabs.Tab;
import it.unipi.di.rasip.gui.tabs.TabMenu;
import it.unipi.di.rasip.protocol.Mediator;
import it.unipi.di.rasip.protocol.RasipException;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;

public class ChatManager extends RASDisplayable implements CommandListener {
	private static ChatManager _instance;

	public static ChatManager getInstance() {
		if (null == _instance) {
			_instance = new ChatManager();
		}
		return _instance;
	}
	
	TabMenu tabs;
	Command nextTabCmd;
	Command prevTabCmd;
	Command newChatCmd;
	Command exitCmd;
	Command sendCmd;
	
	Vector chatList;
	int selectedChat;
	
	private ChatManager() {
		chatList = new Vector();
		
		Form f = new Form("Chat Manager");
		
		displayable = f;
		tabs = new TabMenu(displayable.getWidth());
		
		f.append(tabs);
		tabs.initialize();
		
		newChatCmd = new Command("New chat", Command.ITEM, 3);
		displayable.addCommand(newChatCmd);
		
		exitCmd = new Command("Exit", Command.EXIT, 3);
		displayable.addCommand(exitCmd);
		
		nextTabCmd = new Command("Next Tab", Command.ITEM, 2);
		displayable.addCommand(nextTabCmd);
		prevTabCmd = new Command("Prev Tab", Command.ITEM, 2);
		displayable.addCommand(prevTabCmd);
		
		sendCmd = new Command("Send", Command.ITEM, 1);
		displayable.addCommand(sendCmd);
		
		displayable.setCommandListener(this);
		
		selectedChat = -1;
	}

	public void commandAction(Command cmd, Displayable d) {
		if (cmd == prevTabCmd) {
			this.prevTab();
		} else if (cmd == nextTabCmd) {
			this.nextTab();
		} else if (cmd == exitCmd) {
			Rasip.getInstance().notifyDestroyed();
		} else if (cmd == newChatCmd) {
			//New Chat
			ChatCreator.getInstance().show();
		} else if (cmd == sendCmd) {
			//Send message
			TextField tf = (TextField)((Form)displayable).get(2);
			String msg = tf.getString();
			if(msg.length() > 0) {
				Chat chat = (Chat)(chatList.elementAt(selectedChat));
				try {
					chat.sendMessage(msg);
				} catch (RasipException e) {
					e.printStackTrace();
				}
			}
			tf.setString(null);
		}
	}
	
	public void nextTab() {
		tabs.goRight();
		this.loadSelectedTab();
	}
	
	public void prevTab() {
		tabs.goLeft();
		this.loadSelectedTab();
	}
	
	public void newChat(RasipUser remoteUser, String myUsername) {
		Chat chat = new Chat(remoteUser, myUsername);
		ChatForm cf = new ChatForm();
		chat.setChatForm(cf);
		cf.appendToForm((Form)displayable);
		
		chatList.addElement(chat);
		Tab newTab = new Tab(remoteUser.getUsername());
		tabs.addTab(newTab);
		chat.setTab(newTab);
		Mediator.getInstance().addChat(chat, remoteUser.getCompleteURI());
		this.goToLastTab();
	}
	
	public void newChat(Chat chat) {
		ChatForm cf = new ChatForm();
		chat.setChatForm(cf);
		cf.appendToForm((Form)displayable);
		
		chatList.addElement(chat);
		Tab newTab = new Tab(chat.getRemoteUser().getUsername());
		tabs.addTab(newTab);
		chat.setTab(newTab);
		//Mediator.getInstance().addChat(chat, chat.getRemoteUser().getCompleteURI());
		this.goToLastTab();
	}
	
	public void goToLastTab() {
		tabs.goLast();
		this.loadSelectedTab();
	}
	
	public void loadSelectedTab() {
		selectedChat = tabs.getSelectedTabIndex();
		Form f = (Form)displayable;
		//Elimino pannello e textfield
		f.deleteAll();
		f.append(this.tabs);
		
		((Chat)chatList.elementAt(selectedChat)).getChatForm().appendToForm(f);
	}
}
